package com.xthh._function._ini;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.xthh._enum.HH_EnumReleaseRecordINI;
import com.xthh._function.HH_FileUtil;
import com.xthh._function.HH_SystemUtil;
import com.xthh._struct.HH_T_DeviceColorType;
import com.xthh._struct.HH_T_ReleaseRecord;
import com.xthh._struct.HH_T_TaskInfo;
import com.xthh.sql.HH_ReleaseRecordDao2;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class HH_INIReleaseRecordInfo {

    private static final String TAG = "INIReleaseRecordInfo";

    public static String GetReleaseRecordFolder(Application api) {
        return HH_SystemUtil.GetAppRootFolder(api) + "ReleaseRecord" + File.separator;
    }

    // 读本地发布记录文件
    public static List<HH_T_ReleaseRecord> GetLoaclReleaseRecordInfo(Context ctx, Application api) {
        // 从数据库读
        HH_ReleaseRecordDao2 dao = new HH_ReleaseRecordDao2(ctx, api);
        List<HH_T_ReleaseRecord> list = dao.queryAll();
        return list; 
    }

    // 写发布记录
    public static HH_T_ReleaseRecord WriteReleaseRecordToFile(HH_T_TaskInfo ti,
                                                              boolean writeLastLogOnly,
                                                              Context ctx,
                                                              Application api,
                                                              List<HH_T_DeviceColorType> colorType) {
        if (ti == null || ti.getTaskID() == null || ctx == null)
            return null;


        //.xttempbl/ReleaseRecord/年月日/任务ID/MAC地址/下面的各种参数
        String strWritePath = String.format("%s%s%04d%02d%02d",
                GetReleaseRecordFolder(api),
                File.separator,
                ti.getMadeTime().getYear(), ti.getMadeTime().getMonth(), ti.getMadeTime().getDay());
        HH_FileUtil.CreateFolder(strWritePath);

        strWritePath += File.separator;
        strWritePath += ti.getTaskID();
        HH_FileUtil.CreateFolder(strWritePath);
        strWritePath += File.separator;
        strWritePath += ti.getMacRes();
        HH_FileUtil.CreateFolder(strWritePath);
        strWritePath += File.separator;

        HH_T_ReleaseRecord tr = new HH_T_ReleaseRecord();
        tr.setMac(ti.getMacRes());
        tr.setTaskID(ti.getTaskID());
        if (ti.getTaskType() != null)
            tr.setTaskType(String.format("%d", ti.getTaskType().ordinal()));
        tr.setRes(String.format("%d*%d", ti.getPicW(), ti.getPicH()));
        if (ti.getDeviceColorType() != null){
            String colorTypeTxt = HH_SystemUtil.GetDeviceColorTxtInfo(ti.getDeviceColorType(), colorType);
            tr.setColor(colorTypeTxt);
        }
        tr.setTaskState(String.format("%d", ti.getTaskRunState()));
        tr.setTime(String.format("%04d%02d%02d%02d%02d%02d",
                ti.getMadeTime().getYear(),
                ti.getMadeTime().getMonth(),
                ti.getMadeTime().getDay(),
                ti.getMadeTime().getHour(),
                ti.getMadeTime().getMinute(),
                ti.getMadeTime().getSecond()));
        tr.setPicPath_A(ti.getPicPath_A());
        tr.setPicPath_B(ti.getPicPath_B());
        tr.setTagName(ti.getName());
        tr.setLogPath(String.format("%s", ti.getTaskLogPath()));
        tr.setMsg_title(String.format("%s", ti.getMsgTask() == null ? "" : ti.getMsgTask().getMsgTaskTitle()));
        tr.setMsg_content(String.format("%s", ti.getMsgTask() == null ? "" : ti.getMsgTask().getMsgTaskContnet()));
        tr.setLed_control_type(String.format("%d", ti.getLEDTask() == null ? 0 : ti.getLEDTask().getControlType()));
        tr.setLed_light_type(String.format("%d", ti.getLEDTask() == null ? 0 : ti.getLEDTask().getLightType().ordinal()));
        tr.setLed_duration_second(String.format("%d", ti.getLEDTask() == null ? 0 : ti.getLEDTask().getDurationSeconds()));
        tr.setLastLog(String.format("%s", ti.getTaskLastLog()));

        tr.setOtaPath(ti.getOtaPath());
        tr.setMsgTaskID(String.format("%d", ti.getMsgTask() == null ? 0 : ti.getMsgTask().getMsgTaskID()));
        tr.setMsgTaskFontType(String.format("%d", ti.getMsgTask() == null ? 0 : ti.getMsgTask().getMsgTaskFontType()));
        tr.setMsgTaskMsgType(String.format("%d", ti.getMsgTask() == null ? 0 : ti.getMsgTask().getMsgTaskMsgType()));


        if (writeLastLogOnly) {

        } else {

            // 任务类型图片或信息或指令
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.tasktype.toString() + ".txt", tr.getTaskType(), false);
            // 刷图的图片路径
            if (ti.getPicPath_A() != null)
                HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.picpath.toString() + ".txt", tr.getPicPath_A(), false);
            // 刷图的图片路径
            if (ti.getPicPath_B() != null)
                HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.picpath_B.toString() + ".txt", tr.getPicPath_B(), false);
            // 标签名称
            if (ti.getName() != null)
                HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.tagname.toString() + ".txt", tr.getTagName(), false);
            // 标签分辨率
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.res.toString() + ".txt", tr.getRes(), false);
            // 标签颜色
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.color.toString() + ".txt", tr.getColor() == null ? "" : tr.getColor(), false);
            // 执行状态，成功失败进行中
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.taskstate.toString() + ".txt", tr.getTaskState(), false);
            // 最后次执行时间
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.time.toString() + ".txt", tr.getTime(), false);
            // 当前任务详细日志文件
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.logpath.toString() + ".txt", tr.getLogPath(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.msg_title.toString() + ".txt", tr.getMsg_title(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.msg_content.toString() + ".txt", tr.getMsg_content(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.led_control_type.toString() + ".txt", tr.getLed_control_type(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.led_light_type.toString() + ".txt", tr.getLed_light_type(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.led_duration_second.toString() + ".txt", tr.getLed_duration_second(), false);

            if (ti.getOtaPath() != null)
                HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.otaPath.toString() + ".txt", tr.getOtaPath(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.msgTaskID.toString() + ".txt", tr.getMsgTaskID(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.msgTaskFontType.toString() + ".txt", tr.getMsgTaskFontType(), false);
            HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.msgTaskMsgType.toString() + ".txt", tr.getMsgTaskMsgType(), false);

        }

        // 最后的一条任务状态
        HH_FileUtil.WriteFile(ctx, strWritePath + HH_EnumReleaseRecordINI.lastlog.toString() + ".txt", tr.getLastLog(), false);

        return tr;
    }
}
